<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>动态表格</title>

    <style>
        table{
            border: 1px solid;
            margin: auto;
            width: 500px;
        }

        td,th{
            text-align: center;
            border: 1px solid;
        }
        div{
            text-align: center;
            margin: 50px;
        }
    </style>

</head>
<body>

<div>
    <input type="text" id="name" placeholder="请输入姓名" autocomplete="off">
    <input type="text" id="age"  placeholder="请输入年龄" autocomplete="off">
    <input type="text" id="gender"  placeholder="请输入性别" autocomplete="off">
    <input type="button" value="添加" id="add">
</div>

    <table>
        <caption>学生信息表</caption>
        <tr>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>操作</th>
        </tr>

        <tr>
            <td>张三</td>
            <td>23</td>
            <td>男</td>
            <td><a href="JavaScript:void(this);" onclick="drop(this)">删除</a></td>
        </tr>

        <tr>
            <td>李四</td>
            <td>24</td>
            <td>男</td>
            <td><a href="JavaScript:void(this);" onclick="drop(this)">删除</a></td>
        </tr>

    </table>

</body>
<script>
    //添加
    //按钮
    document.querySelector("#add").onclick = function(){
        let nameValue = document.querySelector("#name").value;
        let ageValue = document.querySelector("#age").value;
        let genderValue = document.querySelector("#gender").value;
        //拼接
        let tr = `
            <tr>
                <td>${nameValue}</td>
                <td>${ageValue}</td>
                <td>${genderValue}</td>
                <td><a href="JavaScript:void(0);" onclick="drop(this)">删除</a></td>
            <tr>
        `;
        //添加到表格
        document.querySelector("tbody").innerHTML+=tr;
    }

    //删除
    // document.querySelector("a").setAttribute("href","drop(this)");
    function drop(obj){
        obj.parentElement.parentElement.parentElement.removeChild(obj.parentElement.parentElement);
    }
</script>
</html>